#!/usr/bin/ruby
# By Niklaus Giger niklaus.giger@member.fsf.org
# February 28, 2008
# A small script to install (most) of the *.mo files for the OLPC
#
require 'ftools'
OPT="--no-verbose -nc --no-check-certificate"
languages=['de', 'fr']
origin="https://dev.laptop.org/translate"

activities = { "xo_core" => ['chat-activity','oficina', 'sugar',
    'journal-activity', 'read-activity', 'record-activity',
    'web-activity', 'write'],
    'xo_bundled' => [
        'TamTamEdit.activity',
        'TamTamJam.activity',
        'TamTamMini.activity',
        'TamTamSynthLab.activity',
        'acoustic-measure-activity',
        'calculate-activity',
        'connect-activity',
        'measure_stable',
        'connect-activity',
        'measure_stable',
        'pippy-activity',
        'slider-puzzle'],
    'etoys' => ['etoys'],
}

current = Dir.pwd
puts current
languages.each { |lang|
    puts "lang ist #{lang}" if $VERBOSE
    dest = "#{current}/#{lang}/LC_MESSAGES"
    File.makedirs(dest) if !File.directory?(dest)
    Dir.chdir(dest)
    # puts activities.inspect
    activities.each{ |pack, acts|
    puts acts.inspect if $VERBOSE
    acts.each{ |activity|
        cmd= "wget #{OPT} #{origin}/#{lang}/#{pack}/#{activity}.po" 
        puts cmd if $VERBOSE
        exit 3 if !system(cmd)
        mo = "#{activity}.mo"
        cmd = "msgfmt -o #{mo} #{activity}.po"
        puts cmd if $VERBOSE
        if !File.exists?(mo) then
            exit 3 if !system(cmd)
        end
        dest = "/usr/share/locale/#{lang}/LC_MESSAGES/#{activity}.mo"
        puts "#{dest} #{File.exists?(dest)}"
        }
    }
}
Dir.chdir(current)
exit 3 if !system("tar -cjf mo_files.tar.bz2 */*/*.mo")


Steps_To_Install =<<EOT

Hi!

You just successfully created the file mo_files.tar.bz2.
Now we have to transfer it to the OLPC e.g. by copying
onto a flash stick or via a network copy utility like scp.
A USB flash will be mounted automatically if you insert it
into the OLPC and will appear under a name like /media/MyUsbStick

On the OLPC:
Open the terminal activity
become root by calling "su" (no password needed by default)
Then get the file, e.g. by using
bash-3.2# scp user@10.0.2.1:/path/to/mo_files.tar.bz2 /home/olpc/
Now you have to unpack it
bash-3.2# cd /usr/share/locale
bash-3.2# tar -jvxf /home/olpc/mo_files.tar.bz2

To permanently set the desired language you have:
edit /etc/sysconfig/i18n and change
LANG=C
to something like
LANG=de_CH # für die deutsche Schweiz
or
LANG=de_FR # pour la Suisse romande

Set the correct keyboard mapping yo have to to edit
/etc/keyboard
Set it to
KEYTABLE="sg"
XKB_MODEL="olpc"
XKB_LAYOUT="ch"
XKB_VARIANT=""
Prenez "sf" au lieu de "sg" si vous voulez un clavier Suisse romande.
If you want to change the keyboard layout just for the current session
use "setxkbmap ch".

Set the desired language (German/Switzerland fuer Deutsch or French/Switzerland pour choisir le francais)
$ sugar-control-panel -s language German/Switzerland

Now we have to restart the X-server, e.g. 
by pressing ctrl-alt-erase
by calling as root killall X 
by rebooting

EOT

puts Steps_To_Install
